package org.corolla.cloth.web.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.corolla.cloth.entity.Order;
import org.corolla.cloth.service.OrderService;

import sun.tools.tree.ThisExpression;

import com.opensymphony.xwork2.ActionSupport;

@ParentPackage(value = "struts-default")
@Results({
		@Result(name = "input", type = "redirect", location = "/view/Car.jsp"),
		@Result(name = "success", type = "redirect", location = "/view/Car.jsp") })
@Namespace("/view")
@Action(value = "updateAction")
public class UpdateAction extends ActionSupport {

	@Resource(name = "orderService")
	private OrderService orderService;

	private String total;
	private String count;
	private String orderTotal;
	private String producttotaltxt;
	private String allttotaltxt;

	@Override
    public String execute() throws Exception {
    	orderService=new OrderService();
        // 程序运行到execute()时，前面的验证都已经正确了
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        session.setAttribute("orderTotal", this.getOrderTotal());
        List<Order> orderlist = orderService.findAllOrders();
        String clothcount[] = this.getCount().split("_");
        String clothtotal[] = this.getTotal().split("_");
        try {
            if (null != orderlist) {
                if (orderlist.size() > 0) {
                    for (int i = 0; i < orderlist.size(); i++) {
                        orderlist.get(i).setClothMount(Integer.valueOf(clothcount[i]));
                        orderlist.get(i).setTotalPriceBySize(Double.parseDouble(clothtotal[i]));
                        orderService.upadteCountByPk(orderlist.get(i));
                    }
                }

            }
            session.setAttribute("orderlist", orderlist);
            session.setAttribute("orderTotal", this.getOrderTotal());
            session.setAttribute("totelprice", this.getProducttotaltxt());
            session.setAttribute("totalcountprice", this.getAllttotaltxt());
            return SUCCESS;
        } catch (Exception ex) {
            ex.printStackTrace();
            return "input";
        }
    }

	public String getTotal() {
		return total;
	}

	public void setTotal(String total) {
		this.total = total;
	}

	public String getCount() {
		return count;
	}

	public void setCount(String count) {
		this.count = count;
	}

	public String getOrderTotal() {
		return orderTotal;
	}

	public void setOrderTotal(String orderTotal) {
		this.orderTotal = orderTotal;
	}

	public String getProducttotaltxt() {
		return producttotaltxt;
	}

	public void setProducttotaltxt(String producttotaltxt) {
		this.producttotaltxt = producttotaltxt;
	}

	public String getAllttotaltxt() {
		return allttotaltxt;
	}

	public void setAllttotaltxt(String allttotaltxt) {
		this.allttotaltxt = allttotaltxt;
	}
	
}
